package formulation.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import db.CommonObject;
import db.CommonService;
import db.formulation.Formulation;
import db.formulation.FormulationService;
import db.fortifier.Fortifier;
import db.fortifier.FortifierService;
import db.nutriment.Nutriment;
import db.nutriment.NutrimentService;
import db.standard.Standard;
import db.standard.StandardService;
import db.standard.classification.Classification;
import db.standard.classification.ClassificationService;
import db.vector.VectorService;

public class FrameController extends MultiActionController
{  
    CommonService commonService;
    FormulationService formulationService;
    FortifierService fortifierService;
    NutrimentService nutrimentService;
    StandardService standardService;
    VectorService vectorService;
    ClassificationService classificationService; 
    
    public void setClassificationService(ClassificationService classificationService) {
        this.classificationService = classificationService;
    }
    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }
    
    public void setFormulationService(FormulationService formulationService) {
        this.formulationService = formulationService;
    }
    
    public void setFortifierService(FortifierService fortifierService) {
        this.fortifierService = fortifierService;
    }
    
    public void setNutrimentService(NutrimentService nutrimentService) {
        this.nutrimentService = nutrimentService;
    }
    
    public void setStandardService(StandardService standardService) {
        this.standardService = standardService;
    }
    
    public void setVectorService(VectorService vectorService) {
        this.vectorService = vectorService;
    }
    
    public ModelAndView index(HttpServletRequest request,
            HttpServletResponse response)
    {
        return new ModelAndView("frame/index");
    }
    
    public ModelAndView search(HttpServletRequest request,
            HttpServletResponse response)
    {
    	String converter = request.getParameter("id");
        String queryString = request.getQueryString();
        String key = request.getParameter("search_key");
        String strOffset = request.getParameter("pager.offset");
        int offset = 0;
        if (strOffset != null) {
            offset = Integer.parseInt(strOffset);
        }
        
        long listCount = 0;
        
        if(converter == null)
        {
        	List<CommonObject> listAll = null;
        	listAll = commonService.search(key, offset, 10);
        	listCount = commonService.searchCount(key);
        	request.setAttribute("result", listAll);
        }else{
        	if(Long.parseLong(converter) == 0)
        	{
        		List<CommonObject> listAll = null;
        		listAll = commonService.search(key, offset, 10);
        		listCount = commonService.searchCount(key);
        		request.setAttribute("result", listAll);
        	}
        	if(Long.parseLong(converter) == 1)
        	{
        		List<Nutriment> listAll = null;
        		listAll = nutrimentService.search(key, offset, 10);
        		listCount = nutrimentService.searchCount(key);
        		request.setAttribute("result", listAll);
        	}
        	if(Long.parseLong(converter) == 2)
        	{
        		List<Fortifier> listAll = null;
        		listAll = fortifierService.search(key, offset, 10);
        		listCount = fortifierService.searchCount(key);
        		request.setAttribute("result", listAll);
        	}
        	if(Long.parseLong(converter) == 3)
        	{
        		List<Standard> listAll = null;
        		listAll = standardService.search(key, offset, 10);
        		listCount = standardService.searchCount(key);
        		request.setAttribute("result", listAll);
        	}
        	if(Long.parseLong(converter) == 4)
        	{
        		List<Formulation> listAll = null;
        		listAll = formulationService.search(key, offset, 10);
        		listCount = formulationService.searchCount(key);
        		request.setAttribute("result", listAll);
        	}
        }
    	request.setAttribute("address", "search.jsp");
    	request.setAttribute("queryString", queryString);
    	request.setAttribute("total", listCount);

    	return new ModelAndView("frame/re");
    }
    
    public  ModelAndView standard_form(HttpServletRequest request,
            HttpServletResponse response)
    {
    	request.setAttribute("address", "../standard/standard_form.jsp");
    	request.setAttribute("standards", standardService.list());
    	request.setAttribute("classifications", classificationService.list());
    	request.setAttribute("nutriments", nutrimentService.list());
    	return new ModelAndView("frame/re");
    }
    public  ModelAndView nutriment_form(HttpServletRequest request,
            HttpServletResponse response)
    {
    	request.setAttribute("address", "../nutriment/nutriment_form.jsp");
    	request.setAttribute("nutriments", nutrimentService.list());
    	return new ModelAndView("frame/re");
    }
    
    public  ModelAndView fortifier_form(HttpServletRequest request,
            HttpServletResponse response)
    {
    	request.setAttribute("address", "../fortifier/fortifier_form.jsp");
    	request.setAttribute("fortifiers", fortifierService.list());
    	request.setAttribute("nutriments", nutrimentService.list());
    	return new ModelAndView("frame/re");
    }
    public  ModelAndView fortifier_popwindow(HttpServletRequest request,
            HttpServletResponse response)
    {
    	return new ModelAndView("fortifier/fortifier_popwindow");
    }
    public ModelAndView formulation_manage_form(HttpServletRequest request,
    		HttpServletResponse reponse )
    {
    	request.setAttribute("address", "../formulation/formulation_manage_form.jsp");
    	request.setAttribute("formulations", formulationService.list());
    	return new ModelAndView("frame/re");
    }
    
    public ModelAndView formulation_edit_form(HttpServletRequest request, HttpServletResponse reponse)
    {
    	request.setAttribute("address", "../formulation/formulation_edit_form.jsp");
    	request.setAttribute("formulations", formulationService.list());
    	request.setAttribute("nutriments", nutrimentService.list());
    	request.setAttribute("fortifiers", fortifierService.list());
    	request.setAttribute("standards", standardService.list());
    	request.setAttribute("vectors", vectorService.list());
    	return new ModelAndView("frame/re");
    }
    public ModelAndView popwindow(HttpServletRequest request,
    		HttpServletResponse reponse )
    {
    	String type = request.getParameter("type");
    	String id = request.getParameter("id");
    	if(type.equals("Nutriment"))
    	{
    		Nutriment nu = nutrimentService.find(Long.parseLong(id));
    		request.setAttribute("id",nu.getId()+"");
    		request.setAttribute("name",nu.getName());
    		request.setAttribute("unit",nu.getUnit());
    		request.setAttribute("description",nu.getDescription());
    		request.setAttribute("nutriments",nutrimentService.list());
    		return new ModelAndView("/nutriment/nutriment_popwindow");
    	}
    	else if(type.equals("Standard"))
    	{
    		Standard st = standardService.find(Long.parseLong(id));
    		
    		request.setAttribute("id",st.getId()+"");
    		request.setAttribute("classifications", classificationService.list());
    		request.setAttribute("nutriments",nutrimentService.list());
    		request.setAttribute("nutriment",st.getNutriment().getId()+"");
    		request.setAttribute("classification",st.getClassification().getId()+"");
    		request.setAttribute("max",st.getMax()+"");
    		request.setAttribute("min",st.getMin()+"");
    		request.setAttribute("description",st.getDescription());
    		return new ModelAndView("/standard/standard_popwindow");
    	}
    	else if(type.equals("Fortifier"))
    	{
    		Fortifier fo = fortifierService.find(Long.parseLong(id));
    		request.setAttribute("id",fo.getId()+"");
    		request.setAttribute("name",fo.getName());
    		request.setAttribute("unit",fo.getUnit());
    		request.setAttribute("cost",fo.getCost()+"");
    		request.setAttribute("nutriments",fo.getNutriments());
    		request.setAttribute("purity",fo.getPurity()+"");
    		request.setAttribute("loss",fo.getLoss()+"");
    		request.setAttribute("ratio",fo.getRatio()+"");    		
    		request.setAttribute("description",fo.getDescription());
    		return new ModelAndView("/fortifier/fortifier_popwindow");
    	}
    	else
    	{return new ModelAndView(type);}
    }
    public ModelAndView returnToIndex(HttpServletRequest request, HttpServletResponse reponse)
    {
    	return new ModelAndView("frame/index");
    }
}

